package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BieRecords;
import com.ruoyi.system.service.IBieRecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 访问记录Controller
 * 
 * @author zxl
 * @date 2021-05-03
 */
@RestController
@RequestMapping("/system/records")
public class BieRecordsController extends BaseController
{
    @Autowired
    private IBieRecordsService bieRecordsService;

    /**
     * 查询访问记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(BieRecords bieRecords)
    {

        autoHandlerComeOut();
        startPage();
        List<BieRecords> list = bieRecordsService.selectBieRecordsList(bieRecords);
        return getDataTable(list);
    }

    /**
     * 导出访问记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:records:export')")
    @Log(title = "访问记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BieRecords bieRecords)
    {
        List<BieRecords> list = bieRecordsService.selectBieRecordsList(bieRecords);
        ExcelUtil<BieRecords> util = new ExcelUtil<BieRecords>(BieRecords.class);
        return util.exportExcel(list, "访问记录数据");
    }

    /**
     * 获取访问记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:records:query')")
    @GetMapping(value = "/{recordsId}")
    public AjaxResult getInfo(@PathVariable("recordsId") String recordsId)
    {
        return AjaxResult.success(bieRecordsService.selectBieRecordsById(recordsId));
    }

    /**
     * 新增访问记录
     */
    @PreAuthorize("@ss.hasPermi('system:records:add')")
    @Log(title = "访问记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BieRecords bieRecords)
    {
        int row = bieRecordsService.insertBieRecords(bieRecords);
        if(row>0){
            return AjaxResult.success(row);
        }else if(row==-1){
            return AjaxResult.error("该门禁卡已经在黑名单里，禁止使用，已警报");
        }else{
            return AjaxResult.error("其他错误");
        }


        //return toAjax();
    }

    /**
     * 修改访问记录
     */
    @PreAuthorize("@ss.hasPermi('system:records:edit')")
    @Log(title = "访问记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BieRecords bieRecords)
    {
        return toAjax(bieRecordsService.updateBieRecords(bieRecords));
    }

    /**
     * 删除访问记录
     */
    @PreAuthorize("@ss.hasPermi('system:records:remove')")
    @Log(title = "访问记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordsIds}")
    public AjaxResult remove(@PathVariable String[] recordsIds)
    {
        return toAjax(bieRecordsService.deleteBieRecordsByIds(recordsIds));
    }


    /**
     *
     */
    @GetMapping("/handlerComeOut")
    public AjaxResult autoHandlerComeOut()
    {
        bieRecordsService.autoHandlerComeOut();
        return toAjax(1);
    }
}
